package com.wen.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.mapper.OrdersMapper;
import com.wen.pojo.Orders;
import com.wen.pojo.Pay;
import com.wen.pojo.PayDetails;
import com.wen.pojo.StockDetails;
import com.wen.pojo.dto.OrdersDto;
import com.wen.pojo.dto.wen.*;
import com.wen.mapper.*;
import com.wen.pojo.*;
import com.wen.pojo.dto.*;
import com.wen.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.wen.mapper.ProductMapper;
import com.wen.pojo.dto.ProductAndOrdersAndOrdersDetails;
import com.wen.pojo.dto.ProductAndOrdersAndOrdersDetailsDto;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

   @Autowired
   private OrdersMapper mapper;


    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;


    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private StockDetailsMapper stockDetailsMapper;



    @Override
    public Page<OrdersDto> sel(OrdersDtoTiaoJian dto, Integer pageNum, Integer size) {

        Page<OrdersDto> page = new Page<>(pageNum, size);
        Page<OrdersDto> sel = mapper.sel(page, dto);

        return sel;
    }

    @Override
    public Page<OrdersANDConsumerANDMerchants> selAll(OrdersANDConsumerANDMerchantDto dto, Integer pageNum, Integer size) {

        Page<OrdersANDConsumerANDMerchants> page = new Page<>(pageNum, size);
        Page<OrdersANDConsumerANDMerchants> ordersANDConsumerANDMerchantsPage = mapper.selAll(page, dto);

        return ordersANDConsumerANDMerchantsPage;
    }

    @Override
    public IPage<TransferGoods> searchPage2(Page<TransferGoods> pageParam, TransferGoodsDto dto, Long pageNum, Long pageSize) {
        pageParam = new Page<>(pageNum, pageSize);
        return mapper.searchPage2(pageParam, dto);
    }

    @Override
    public Orders2 ordersByid(String id) {
        return mapper.ordersByid(id);
    }

    @Override
    public List<OrderDetails2> OrderDetailslist(String id) {
        return mapper.OrderDetailslist(id);
    }

    @Override
    public boolean addPay(Pay pay) {
        return mapper.addPay(pay);
    }

    @Override
    public List<StockDetails> stocklist() {
        return mapper.stocklist();
    }

    @Override
    public boolean addPayDetails(PayDetails pay) {
        return mapper.addPayDetails(pay);
    }

    @Override
    public void upStock(PayDetails payDetails) {
        mapper.upStock(payDetails);
    }

    @Override
    public void upStock2(PayDetails payDetails) {
        mapper.upStock2(payDetails);
    }

    @Override
    public void upOrderState(String id) {
        mapper.upOrderState(id);
    }

    @Override
    public IPage<OrdersDto> selOrderState1(Long pageNum, Long pageSize) {
        Page<Object> objectPage = new Page<>(pageNum, pageSize);
        return mapper.selOrderState1(objectPage);
    }

    @Override
    public IPage<OrdersDto> selOrderState2(Long pageNum, Long pageSize) {
        Page<Object> objectPage = new Page<>(pageNum, pageSize);
        return mapper.selOrderState2(objectPage);
    }

    @Override
    public IPage<OrdersDto> selOrderState3(Long pageNum, Long pageSize) {
        Page<Object> objectPage = new Page<>(pageNum, pageSize);
        return mapper.selOrderState3(objectPage);
    }

    @Override
    public Boolean upOrderState3(String id) {
        return mapper.upOrderState3(id);
    }


    @Override
    public Page<ProductAndOrdersAndOrdersDetails> getSelectAll(ProductAndOrdersAndOrdersDetailsDto dto) {

        Page<ProductAndOrdersAndOrdersDetails> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<ProductAndOrdersAndOrdersDetails> productAndOrdersAndOrdersDetailsPage = mapper.selectAll(page, dto);

        return productAndOrdersAndOrdersDetailsPage;
    }

//    @Override
//    public List<ProductAndOrdersAndOrdersDetails> getSelectAll1(ProductAndOrdersAndOrdersDetailsDto dto) {
//
//        List<ProductAndOrdersAndOrdersDetails> productAndOrdersAndOrdersDetails = mapper.selectAll(null, dto);
//
//        return productAndOrdersAndOrdersDetails;
//    }

    @Override
    public int addOrders(Orders orders) {
        orders.setOrderNumber("300202212160003");//订单编号
        orders.setCreatetime(new Date());//创建时间
        orders.setOnOff(0);//是否启用
        orders.setConsumerId(1);//支付标志
        return mapper.insert(orders);
    }

    @Override
    public int Or(OrdersDto ordersDto) {

        ProductAndCart productAndCart = cartMapper.selProductAndCartSum(ordersDto.getConsumerIds());

        Orders orders = new Orders();
        orders.setOrderNumber("300202212160003");//订单编号
        orders.setCreatetime(new Date());//创建时间
        orders.setOnOff(0);//是否启用
        orders.setPaychannel(1);//支付标志
        orders.setConsumerId(ordersDto.getConsumerId());//下单用户id
        orders.setMerchantId(ordersDto.getMerchantId());//商户id
        orders.setAmountnum(Float.valueOf(productAndCart.getSumprice()));//总价
        orders.setTotalnum(Integer.valueOf(productAndCart.getCounts()));//总数
        int insert = mapper.insert(orders);

        List<ProductAndCart> productAndCartsList = cartMapper.selProductAndCartSelected(ordersDto.getConsumerIds());

        for (ProductAndCart productAndCartTwo : productAndCartsList){
            OrderDetails orderDetails = new OrderDetails();
            orderDetails.setProductId(Long.parseLong(productAndCartTwo.getProductid()));//商品id
            orderDetails.setOrdersId(orders.getId());//订单id
            orderDetails.setAmount(productAndCartTwo.getCount());//数量
            orderDetails.setPrice(productAndCartTwo.getPrice());//单价
            orderDetails.setSubtotal(productAndCartTwo.getPrice() * productAndCartTwo.getCount());//小计
            orderDetailsMapper.insert(orderDetails);

            productMapper.UpdateCount(productAndCartTwo.getCount(),productAndCartTwo.getProductid());



//            StockDetails stockDetails = new StockDetails();
//            stockDetails.setRealAmount(stockDetails.getRealAmount() - productAndCartTwo.getCount());
//            QueryWrapper<StockDetails> qw = new QueryWrapper<>();
//            qw.lambda().eq(StockDetails::getProductId,productAndCartTwo.getProductid());
//
//            stockDetailsMapper.update(stockDetails,qw);

        }


        return insert;
    }

    @Override
    public Page<OrderAll> getSelectOrderAll(ProductAndOrdersAndOrdersDetailsDto dto) {
        Page<ProductAndOrdersAndOrdersDetails> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<OrderAll> orderList = mapper.getOrderList(page, dto);

        return orderList;
    }

    @Override
    public List<OrderAll> getSelectOrderById(Integer id) {
        List<OrderAll> orderList = mapper.selectOrderById(id);

        return orderList;
    }
//    @Override
//    public List<ProductAndOrdersAndOrdersDetails> getSelectOrderById(Integer id) {
//
//        return ordersMapper.selectOrderById(id);
//    }



    @Override
    public Page<DiaoBos> seldb(Integer pageNum, Integer size) {
        Page<OrdersANDConsumerANDMerchants> page = new Page<>(pageNum, size);
        Page<DiaoBos> seldb = mapper.seldb(page);

        return seldb;
    }

}

